﻿Imports Microsoft.VisualBasic
Imports log4net

Public Class Utilidades

    Private ReadOnly log As ILog = LogManager.GetLogger("LogFile")

    Public Sub CheckVersion()

        Dim version As Version = Nothing

        Try
            Dim versionCrn As VersionCRN = New VersionCRN()
            version = versionCrn.GetVersion()
        Catch ex As Exception
            log.Error("No se pudo obtener la version de la base de datos", ex)
            HttpContext.Current.Session("ERROR_MESSAGE") = "No se pudo obtener los datos de la version de la Base de datos. Por favor contacte con el Administrador."
            HttpContext.Current.Response.Redirect("~/FatalError.aspx")
        End Try

        Dim versionMayor As Integer
        Dim versionMenor As Integer
        Dim release As Integer
        Try
            versionMayor = CInt(ConfigurationManager.AppSettings("VersionMayor"))
            versionMenor = CInt(ConfigurationManager.AppSettings("VersionMenor"))
            release = CInt(ConfigurationManager.AppSettings("Release"))
        Catch ex As Exception
            log.Error("No se pudo obtener la version de la aplicacion", ex)
            HttpContext.Current.Session("ERROR_MESSAGE") = "No se pudo obtener los datos de la version de la Aplicacion. Por favor contacte con el Administrador."
            HttpContext.Current.Response.Redirect("~/FatalError.aspx")
        End Try

        Dim isCorrect As Boolean = Not version Is Nothing And version.VersionMayor = versionMayor And _
            version.VersionMenor = versionMenor And version.Release = release

        If Not isCorrect Then
            HttpContext.Current.Response.Redirect("~/FatalError.apx")
            HttpContext.Current.Session("ERROR_MESSAGE") = "La version de la Aplicacion no coincide con la version de la base de Datos. Por favor contacte con el Administrador."
            HttpContext.Current.Response.Redirect("~/FatalError.aspx", True)
        End If

    End Sub

End Class
